package config

import (
	"log"
	"os"

	yaml3 "gopkg.in/yaml.v3"
)

// Config 配置
type Config struct {
	DB    Db    `yaml:"db"`
	Redis Redis `yaml:"redis"`
}

// DB 数据库配置
type Db struct {
	EnableLog          bool   `yaml:"enable_log"`
	Dialect            string `yaml:"dialect"`
	Host               string `yaml:"host"`
	User               string `yaml:"user"`
	Pwd                string `yaml:"pwd"`
	Db                 string `yaml:"db"`
	MaxOpenConnections int    `yaml:"max_open_connections"`
	MaxIdleConnections int    `yaml:"max_idle_connections"`
}

// redis
type Redis struct {
	Host string `yaml:"host"`
	Pwd  string `yaml:"pwd"`
	Db   int    `yaml:"db"`
}

// 获取db配置
func GetDb() Db {
	return config.DB
}

func GetRedis() Redis {
	return config.Redis
}

var config *Config

func LoadConfig(path string) {
	//pathStr, _ := filepath.Abs(filepath.Dir(os.Args[0]))
	yamlFile, err := os.ReadFile(path)
	if err != nil {
		log.Fatalf("yamlFile. Get err #%v", err)
	}

	config = &Config{}
	err = yaml3.Unmarshal(yamlFile, config)
	if err != nil {
		log.Fatalf("Unmarshal: %v", err)
	}
}
